package jgcp.master.scheduler;

import java.util.*; 
import jgcp.common.Task;

public class Sort {
	
	private Queue<Task> taskList;
	
	public Queue<Task> sortA(Queue<Task> newTaskList){
		this.taskList=newTaskList;
		int size=taskList.size();
		Task[] taskArray=taskList.toArray(new Task[size]);
		taskList.clear();
		int y,z;
		for(y=0;y<size-1;y++){
			int min=y;
			for(z=y+1;z<size;z++){
				if(taskArray[z].getDeadline().getTime()<(taskArray[min].getDeadline().getTime())){
					min=z;
				}
			Task temp;
			temp=taskArray[y];
			taskArray[y]=taskArray[min];
			taskArray[min]=temp;
			}
		}
		for(int i=0;i<size;i++){
			Task c=taskArray[i];
			taskList.add(c);
		}
		/*
		 * return the queue whose tasks' priorities have been assigned.
		 */
		AssignPriority ap=new AssignPriority();
		taskList=ap.setTaskPriority(taskList);
		return taskList;
	}
	
	public Queue<Task> sortB(Queue<Task> newTaskList){
		this.taskList=newTaskList;
		int size=taskList.size();
		Task[] taskArray=taskList.toArray(new Task[size]);
		taskList.clear();
		int y,z;
		for(y=0;y<size-1;y++){
			int min=y;
			for(z=y+1;z<size;z++){
				if(((taskArray[z].getDeadline().getTime()*0.5)+(taskArray[z].getBudget()*0.5))<((taskArray[min].getDeadline().getTime()*0.5)+(taskArray[min].getBudget()*0.5))){
					min=z;
				}
			Task temp;
			temp=taskArray[y];
			taskArray[y]=taskArray[min];
			taskArray[min]=temp;
			}
		}
		for(int i=0;i<size;i++){
			Task c=taskArray[i];
			taskList.add(c);
		}
		/*
		 * return the queue whose tasks' priorities have been assigned.
		 */
		AssignPriority ap=new AssignPriority();
		taskList=ap.setTaskPriority(taskList);
		return taskList;
	}
}
